package selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SeleniumProxyIp2 {
	public static void main(String[] args) throws InterruptedException {
		// 设置代理
		String proxyIpAndPort= "114.115.140.25:3128";  
		  
		// 代理配置  
		DesiredCapabilities cap = new DesiredCapabilities();  
		org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();  
		// 配置http、ftp、ssl代理（注：当前版本只支持所有的协议公用http协议，下述代码等同于只配置http）  
		proxy.setHttpProxy(proxyIpAndPort)  
		     .setFtpProxy(proxyIpAndPort)  
		     .setSslProxy(proxyIpAndPort);  
		  
		// 以下三行是为了避免localhost和selenium driver的也使用代理，务必要加，否则无法与iedriver通讯  
		cap.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);  
		cap.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);  
		System.setProperty("http.nonProxyHosts", "localhost");  
		  
		cap.setCapability(CapabilityType.PROXY, proxy);  
		WebDriver driver = new ChromeDriver(cap);  
		driver.get("https://www.baidu.com/s?wd=ip");  
		//webDriver.close();
		//webDriver.quit();
	}
}
